﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SZSLSystem.Model;
using SZSLSystem.Common;
using SZSLSystem.DBUtility;

namespace SZSLSystem.DAL
{
    /// <summary>
    /// 数据访问层：角色权限
    /// </summary>
    public partial class RoleAuthority
    {
        public RoleAuthority()
        {

        }

        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RoleAuthority");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 返回数据总数(分页用到)
        /// </summary>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) as H ");
            strSql.Append(" from RoleAuthority ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SZSLSystem.Model.RoleAuthority model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RoleAuthority( ");
            strSql.Append("[ID],[RoleID],[AuthorityID],[Sort])");
            strSql.Append("values (@ID,@RoleID,@AuthorityID,@Sort)");
            SqlParameter[] parameters = {
					new SqlParameter ("@ID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@RoleID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@AuthorityID",SqlDbType.UniqueIdentifier),
                    new SqlParameter ("@Sort",SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RoleID;
            parameters[2].Value = model.AuthorityID;
            parameters[3].Value = model.Sort;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SZSLSystem.Model.RoleAuthority model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RoleAuthority set ");
            strSql.Append("[RoleID]=@RoleID,");
            strSql.Append("[AuthorityID]=@AuthorityID,");
            strSql.Append("[Sort]=@Sort");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter ("@ID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@RoleID",SqlDbType.UniqueIdentifier),
					new SqlParameter ("@AuthorityID",SqlDbType.UniqueIdentifier),
                    new SqlParameter ("@Sort",SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RoleID;
            parameters[2].Value = model.AuthorityID;
            parameters[3].Value = model.Sort;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete RoleAuthority ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeletebyRoleID(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete RoleAuthority ");
            strSql.Append(" where RoleID=@RoleID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SZSLSystem.Model.RoleAuthority GetModel(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from RoleAuthority ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            SZSLSystem.Model.RoleAuthority model = new SZSLSystem.Model.RoleAuthority();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["ID"].ToString() != "")
                {
                    model.ID = (Guid)row["ID"];
                }
                model.RoleID = Guid.Parse(row["RoleID"].ToString());
                model.AuthorityID = Guid.Parse(row["AuthorityID"].ToString());
                model.Sort = int.Parse(row["Sort"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from RoleAuthority ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" from RoleAuthority ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder)
        {
            int topSize = pageSize * currentPage;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pageSize + " * from RoleAuthority");
            strSql.Append(" where Id not in(select top " + topSize + " Id from RoleAuthority");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder + ")");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);

            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
